import { Component, ViewChild } from '@angular/core';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { TranslateService } from '@ngx-translate/core';
import { Config, Nav, Platform } from 'ionic-angular';

import {FirstRunPage, MainPage} from '../pages/pages';
import { Settings } from '../providers/providers';
import {NoticePage} from "../pages/notice/notice";

@Component({
  template: `<ion-menu [content]="content">
    <ion-header>
      <ion-toolbar>
        <ion-title>Pages</ion-title>
      </ion-toolbar>
    </ion-header>

    <ion-content>
      <ion-list>
        <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
          {{p.title}}
        </button>
      </ion-list>
    </ion-content>

  </ion-menu>
  <ion-nav #content [root]="rootPage"></ion-nav>`
})
export class MyApp {
  get pages(): any[] {
    return this._pages;
  }

  set pages(value: any[]) {
    this._pages = value;
  }
  rootPage;

  @ViewChild(Nav) nav: Nav;
  private _pages: any[] = [
    { title: 'Tutorial', component: 'TutorialPage' },
    { title: 'Cards', component: 'CardsPage' },
    { title: 'Tabs', component: 'TabsPage' },
    { title: 'Content', component: 'ContentPage' },
    { title: 'Login', component: 'LoginPage' },
    { title: 'Signup', component: 'SignupPage' },
    { title: 'ListMaster', component: 'ListMasterPage' },
    { title: 'Dmt', component: 'DmtPage' },
    { title: 'Menu', component: 'MenuPage' },
    { title: 'Settings', component: 'SettingsPage' },
    { title: 'Recycling', component: 'RecyclingPage' },
    { title: 'OverView', component: 'OverViewPage' },
    { title: 'OverPath', component: 'OverPathPage' },
    { title: 'OverDetails', component: 'OverDetailsPage' },
    { title: 'Welcome', component: 'WelcomePage' },
    { title: 'QuestionCheckbox', component: 'QuestionCheckboxPage' },
    { title: 'QuestionCity', component: 'QuestionCityPage' },
    { title: 'QuestionSingle', component: 'QuestionSinglePage' },
    { title: 'PhoneRecord', component: 'PhoneRecordPage' },
    { title: 'QuestionArea', component: 'QuestionAreaPage' },
    { title: 'QuestionVote', component: 'QuestionVotePage' },
    { title: 'QuestionMap', component: 'QuestionMapPage' },
    { title: 'QuestionJob', component: 'QuestionJobPage' },
    { title: 'QueationLinkage', component: 'QueationLinkagePage' },
    { title: 'QuestionCommodity', component: 'QuestionCommodityPage' },
    { title: 'QuestionCompletion', component: 'QuestionCompletionPage' },
    { title: 'QuestionImages', component: 'QuestionImagesPage' },
    { title: 'QuestionProportion', component: 'QuestionProportionPage' },
    { title: 'QuestionScore', component: 'QuestionScorePage' },
    { title: 'QuestionTime', component: 'QuestionTimePage' },
    { title: 'Search', component: 'SearchPage' },
    { title: 'Tips', component: 'TipsPage' },
    { title: 'QuestionTestCompletion', component: 'QuestionTestCompletionPage' },
    { title: 'QuestionTestSingle', component: 'QuestionTestSinglePage' },
    { title: 'QuestionMatrix', component: 'QuestionMatrixPage' },
    { title: 'QuestionTestCheckbox', component: 'QuestionTestCheckboxPage' },
    { title: 'QuestionSubjectivity', component: 'QuestionSubjectivityPage' },
    { title: 'QuestionMatrixSingle', component: 'QuestionMatrixSinglePage' },
    { title: 'QuestionMatrixChebox', component: 'QuestionMatrixCheboxPage' },
    { title: 'QuestionMatrixSelect', component: 'QuestionMatrixSelectPage' },
    { title: 'QuestionMatrixScore', component: 'QuestionMatrixScorePage' },
    { title: 'QuestionMatrixSlide', component: 'QuestionMatrixSlidePage' },
    { title: 'QuestionMatrixText', component: 'QuestionMatrixTextPage' },
    { title: 'QuestionMatrixproportion', component: 'QuestionMatrixProportionPage' },
    { title: 'Notice', component: 'NoticePage' },
    { title: 'Goon', component: 'GoonPage' },


  ]

  constructor(private translate: TranslateService,
              platform: Platform,
              settings: Settings,
              private config: Config,
              private statusBar: StatusBar,
              private splashScreen: SplashScreen,
              ) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.statusBar.styleLightContent();
      this.splashScreen.hide();

      if(sessionStorage.getItem('userid') != null){
        this.nav.setRoot(MainPage);
      }
      else{
        this.nav.setRoot(FirstRunPage);
      }

    });
    this.initTranslate();
    // this.rootPage="ListMasterPage";
  }

  initTranslate() {
    // Set the default language for translation strings, and the current language.
    this.translate.setDefaultLang('en');
    const browserLang = this.translate.getBrowserLang();

    if (browserLang) {
      if (browserLang === 'zh') {
        const browserCultureLang = this.translate.getBrowserCultureLang();

        if (browserCultureLang.match(/-CN|CHS|Hans/i)) {
          this.translate.use('zh-cmn-Hans');
        } else if (browserCultureLang.match(/-TW|CHT|Hant/i)) {
          this.translate.use('zh-cmn-Hant');
        }
      } else {
        this.translate.use(this.translate.getBrowserLang());
      }
    } else {
      this.translate.use('en'); // Set your language here
    }

    this.translate.get(['BACK_BUTTON_TEXT']).subscribe(values => {
      this.config.set('ios', 'backButtonText', values.BACK_BUTTON_TEXT);
    });

  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    this.nav.setRoot(page.component);
  }
}
